package com.controller;

/**
 * @author Administration
 * @date 2021/4/14 - 16:06 - 星期三
 * @project lambda-stuty
 * @package com.controller
 * @by GGbomd猪猪侠
 */

import com.pojo.Student;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Consumer<T> : 消费性接口
 *         void accept(T t);
 *
 * Supplier<T > : 供给型接口
 *         T get（）;
 *
 * Function<T,R> : 函数型接口
 *        R apply(T);
 *
 * Predicate<T> : 断言型接口
 *        boolean test(T t);
 */
public class TextLambda3 {


    //消费型接口
    public void consumer(Integer sum , Consumer<Integer> consumer){
        consumer.accept(sum);
    }

    @Test
    public void test(){
        consumer(110, (sum) -> System.out.println("消费" + sum));
    }

    //供给型接口
    public List<Integer> getNumList(Integer num, Supplier<Integer> Supplier){
        List<Integer> integers = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            Integer integer = Supplier.get();
            integers.add(integer);
        }
        return integers;
    }


    @Test
    public void test2(){
        List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
        for (Integer integer : numList) {
            System.out.println(integer);
        }
    }

    // 函数型接口
    public String strHandler(String string, Function<String, String> function){
        return function.apply(string);
    }

    @Test
    public void test3(){
        String s = strHandler(" 不知道 ", (string) -> string.trim());
        System.out.println(s);
    }

    // 断言型接口
     public List<String> strings(List<String> string, Predicate<String> stringPredicate){
        List<String> list = new ArrayList<>();

         for (String s : string) {
             if (stringPredicate.test(s)){
                 list.add(s);
             }
         }
         return list;
     }

     @Test
    public void test4(){
        List<String> list = Arrays.asList("hello", "atmeizi", "ok", "www", "adsfasdfads");
         List<String> strings = strings(list, (string) -> string.length() > 3);
         for (String string : strings) {
             System.out.println(string);
         }
     }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("ss");
        if (list.isEmpty()){
            System.out.println("2222");
        }
        List<Student> list1 = new ArrayList<>();
    }

    @Test
    public void test5(){
        List<Student> list = Arrays.asList(
                new Student(12,"不知道0",55),
                new Student(13,"不知道1",55),
                new Student(14,"不知道2",77),
                new Student(15,"不知道3",88),
                new Student(16,"不知道4",99)
        );
        // 计算一个list中重复得元素  Collectors.counting() 进行一种归约运算运算前面相同得年龄出现了几次
        Map<Object, Long> collect2 = list.stream().collect(
                Collectors.groupingBy( Student::getAge , Collectors.counting()  )   );
        System.out.println("年龄重复计数情况："+ collect2);
        //筛出有重复的姓名  collect2.keySet()获取到它得key  collect2.get(key)通过key获取value值 看value值是否大于1
        List<Object> collect3 = collect2.keySet().stream().
                filter(key -> collect2.get(key) > 1).collect(Collectors.toList());
        System.out.println("重复的年龄 ： " + collect3);


        IntSummaryStatistics collect = list.stream()
                .collect(Collectors.summarizingInt(Student::getAge));
        System.out.println(collect.getMax());

        Map<Integer, List<Student>> collect1 = list.stream()
                .collect(Collectors.groupingBy(Student::getAge));
        System.out.println(collect1);
    }

    @Test
    public void test6(){
        List<Map<Integer, String>> mapList = new ArrayList<>();
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"123");
        mapList.add(map);
            for (Map<Integer, String> integerStringMap : mapList) {
            integerStringMap.forEach((key, value) -> {
                System.out.println(key + "." + value);
            });
        }
        for (Map<Integer, String> integerStringMap : mapList) {
            for (Integer integer : integerStringMap.keySet()) {
                System.out.println(integer + "." + integerStringMap.get(integer));
            }
        }
    }

    @Test
    public void test7(){
        BigDecimal c = new BigDecimal(100.12);
        BigDecimal b = new BigDecimal(308.12);
        BigInteger bigInteger = c.divide(b, 2, BigDecimal.ROUND_HALF_UP).toBigInteger();
        BigInteger a = new BigInteger(String.valueOf(3));
        if (bigInteger.equals(a)){
            System.out.println(bigInteger);
        }

        Integer integer = new Integer(2);
        double v = integer.doubleValue();
        System.out.println(v);
        BigDecimal multiply = c.multiply(b);
        BigDecimal bigDecimal = multiply.setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println(bigDecimal);

        BigDecimal subtract = b.subtract(c);
        BigDecimal add = b.add(c);
        System.out.println(subtract);
        System.out.println(add);
    }

    @Test
    public void test8(){
        List<Student> list1 = new ArrayList<>();
        list1.add(new Student(1,"123",12345));
        list1.add(new Student(2,"123",12345));
        list1.add(new Student(3,"123",12345));
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"123",12345));
        list1.forEach((x) ->  {
            List<Student> collect = list.stream()
                    .filter((y) -> !y.getId().equals(x.getId()))
                    .collect(Collectors.toList());
            System.out.println(collect);
        });
    }

    @Test
    public void test9(){
        List<Student> list1 = new ArrayList<>();
        list1.add(new Student(1,"123",12345));
        list1.add(new Student(2,"123",12345));
        list1.add(new Student(3,"123",12345));
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"123",12345));
        List<Student> listss = new ArrayList<>();
        list1.forEach((x) -> list.forEach((y) -> {
            if (!x.getId().equals(y.getId())){
                listss.add(new Student(x.getId(),x.getName(),x.getAge()));
            }
        }));
        for (Student student : listss) {
            System.out.println(student);
        }
    }

    @Test
    public void test10(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"123",123455));
        list.add(new Student(2,"123",123445875));
        list.add(new Student(3,"123",288989345));
        String name = "梅子";
        String phone = "12345678912";
        String s = name + "(" + phone + ","  + ")";
        StringBuilder s2 = new StringBuilder("我(");
        for (int i = 0; i < list.size(); i++) {
            if (i!=list.size()-1){
                s2.append(list.get(i).getAge()).append(",");
            }else {
                s2.append(list.get(i).getAge());
            }

        }
        s2.append(")");
        System.out.println(s2);

    }

    @Test
    public void test11(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"123",123455));
        list.add(new Student(2,"123",123445875));
        list.add(new Student(3,"123",288989345));
        IntSummaryStatistics collect = list.stream()
                .collect(Collectors.summarizingInt(Student::getId));
        System.out.println(collect.getMax());

        Map<Integer, List<Student>> collect1 = list.stream()
                .collect(Collectors.groupingBy(Student::getId));
        collect1.forEach((x,y) -> {
            y.forEach((l) -> {
                System.out.println(x + "asdfasdfasf" + l);
            });
        });
    }

    @Test
    public void test12(){
        Stream<List<Integer>> listStream = Stream.of(
                Arrays.asList(1),
                Arrays.asList(2, 3),
                Arrays.asList(4, 5, 6)
        );
        List<Integer> collect = listStream.flatMap(Collection::stream)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test15(){
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        test16(integerList);
        for (Integer integer : integerList) {
            System.out.println(integer);
        }
    }

    private void test16(List<Integer> integerList) {
        integerList.add(2);
    }

    @Test
    public void test13(){
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        for (Integer integer : integerList) {
            if (integer == 2){
                HashSet<Integer> hashSet = new HashSet<>();
                hashSet.add(6);
                hashSet.add(7);
                integerList = new ArrayList<>(hashSet);
            }
        }
        for (Integer integer : integerList) {
            System.out.println(integer);
        }
    }

    @Test
    public void test16(){
        String str = "-1/1/2/3/15/12345";
        int i = str.lastIndexOf("/");
        System.out.println(i);
        // 截取 i 后面所有的数字
        String substring = str.substring(i+1);
        System.out.println(substring);
        Integer integer = Integer.valueOf(substring);
        integer = integer + 1;

        System.out.println(integer);

        // 在获取 i 前面的所有字符
        String substring1 = str.substring(0, i +1);

        System.out.println(substring1);
        String stringNew = substring1 + integer;
        System.out.println(stringNew);
    }

    /**
     * 获取这个自然月的所有天数
     */
    public static final String FORMAT_SHORTER = "yyyy-MM";
    @Test
    public void testDate() throws ParseException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_SHORTER);
        // 格式话日期成String类型
        String format = sdf.format(date);
        System.out.println(format);
        System.out.println("2021-12 : " + getDaysByMonth("2021-12"));
    }

    private List<Integer> getDaysByMonth(String date) {
        Calendar c = Calendar.getInstance();
        c.setTime(parse(date, FORMAT_SHORTER));
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        List<Integer> list = null;
        if(month >= 0 && month <= 11){
            list = new ArrayList<>(31);
            c = Calendar.getInstance();
            c.set(year, month, 1);
            int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i = 0; i <= lastDay; i++) {
                list.add(i);
            }
        }
        return list;
    }

    private Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            // 将Strinf转换成date类型 String必须是指定的格式否则会报错
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取本月第一天0点时间
    @Test
    public void getDate4(){
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(cal.getTime());
        System.out.println(format);
    }

    //获得本月最后一天24点时间
    @Test
    public void getDate5(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(cal.getTime());
        System.out.println(format);
    }

    @Test
    public void getDate6(){
        // 当天开始时间
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
        Date dateStart = Date.from( today_start.atZone( ZoneId.systemDefault()).toInstant());
        System.out.println(dateStart);
        // 当天结束时间
        LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天零点
        Date dateEnd = Date.from( today_end.atZone( ZoneId.systemDefault()).toInstant());
        System.out.println(dateEnd);


    }
}